<template>
  <a-modal
    title="修改租約-租客信息"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['roomId']" /></a-form-item>
        <a-form-item
              label="姓名"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-input placeholder="请输入承租人姓名" v-decorator="['name', {rules: [{required: true, message: '请输入承租人姓名！'}]}]" />
            </a-form-item>
            <a-form-item
              label="聯係方式"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-input placeholder="请输入聯係方式" v-decorator="['phone', {rules: [{required: true, message: '请输入聯係方式！'}]}]" />
            </a-form-item>
            <a-form-item
              label="身份证号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-input placeholder="请输入身份证号" v-decorator="['idCard', {rules: [{required: true, message: '请输入身份证号！'}]}]" />
            </a-form-item>
              <a-form-item
              label="身份証照片"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-row>
                <a-col :span="12" >
                  <a-input v-show="false" v-decorator="['idCardImagesFront']" />
                  <a-upload
                    :customRequest="customRequest1"
                    :multiple="false"
                    :showUploadList="false"
                    :before-upload="beforeUpload"
                    name="file">
                    <a-button><a-icon type="upload"/>上傳正面照片</a-button>
                  </a-upload>
                  <a-spin :spinning="divLoading1">
                    <div>
                      <img :src="src1" style="max-width: 99%;width:100px">
                    </div>
                  </a-spin>
                </a-col>
                <a-col :span="12">
                  <a-input v-show="false" v-decorator="['idCardImagesBack']" />
                  <a-upload
                    :customRequest="customRequest2"
                    :multiple="false"
                    :showUploadList="false"
                    :before-upload="beforeUpload"
                    name="file">
                    <a-button><a-icon type="upload"/>上傳反面照片</a-button>
                  </a-upload>
                   <a-spin :spinning="divLoading2">
                    <div>
                      <img :src="src2" style="max-width: 99%;width:100px">
                    </div>
                  </a-spin>
                </a-col>
              </a-row>
            </a-form-item>
              <a-row :gutter="24">
                <a-col :md="24" :sm="24">
                  <a-form-item
                    label="同住人"
                    :labelCol="labelCol_JG"
                    :wrapperCol="wrapperCol_JG">
                    <a-table
                                size="middle"
                                :columns="columns"
                                :dataSource="leaseLiveData"
                                :pagination="false"
                                :loading="memberLoading"
                              >
                              <template slot="idCardImagesFront" slot-scope="text,record">
                                <div>
                                  <img :src="record.src1" style="max-width: 99%;width:100px">
                                </div>
                              </template>
                               <template slot="idCardImagesBack" slot-scope="text,record">
                                <div>
                                  <img :src="record.src2" style="max-width: 99%;width:100px">
                                </div>
                              </template>
                              <template slot="operation" slot-scope="text, record">
                                <a @click="remove(record.key)">删除</a>
                              </template>
                      </a-table>
                  </a-form-item>
                </a-col>
              </a-row>
              <a-row  type="flex" justify="space-around" align="middle">
                <a-col :span="4">
                  <a-button type="primary" size="small" @click="addYj()" >添加同住人</a-button>
                </a-col>
              </a-row>
          <a-row :gutter="24">
              <a-form-item
              label="收租周期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-input-group compact>
                <a-form-item
                    label=""
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol">
                  <a-select style="width: 150px" v-decorator="['receiptFu', { initialValue: 1, rules: [{ required: true, message: '请选择付款方式！' }]}]">
                    <a-select-option v-for="(item,index) in 18" :key="index" :value="item">付{{ item }}</a-select-option>
                  </a-select>
                </a-form-item>
                 <a-form-item
                    label=""
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol">
                  <a-select style="width: 150px" v-decorator="['receiptYj', { initialValue: 2, rules: [{ required: true, message: '请选择付款周期！' }]}]">
                    <a-select-option :value="1">日</a-select-option>
                    <a-select-option :value="2">月</a-select-option>
                    <a-select-option :value="3">年</a-select-option>
                  </a-select>
                </a-form-item>
              </a-input-group>
            </a-form-item>
            </a-row>
              <a-form-item
                  label="收租日"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                >
                  <a-radio-group placeholder="请选择收租日" @change="onBillEndTypeChange" v-decorator="['billEndType', { initialValue: 0 }]">
                    <a-radio :style="radioStyle" :key="0" :value="0">提前</a-radio>
                    <div>
                      <a-form-item label="">
                        <a-input-number
                        :min="1"
                        :max="365"
                        :disabled="billEndTqDataable"
                        style="width:100%"
                        placeholder="请输入账单收租日提前几日"
                        v-decorator="['billEndTqData', { initialValue: 15 }]"/>
                        相较账单起始日提前几天
                      </a-form-item>
                    </div>
                    <a-radio :style="radioStyle" :key="1" :value="1">固定</a-radio>
                    <div  style="width: 100%">
                      <a-form-item  label="">
                        <a-select :disabled="billEndGdmonthDataDataable"  placeholder="请选择账单收租日固定几月" style="width: 100%" v-decorator="['billEndGdmonthData', { initialValue: 0 }]">
                          <a-select-option  style="width: 100%"  v-for="(item,index) in billEndGdmonthDataData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                        </a-select>
                      </a-form-item>
                      <a-form-item >
                        <a-select :disabled="billEndGdmonthDataDataable"   placeholder="请选择账单收租日固定几日" style="width: 100%" v-decorator="['billEndGddayData', { initialValue: 5 }]">
                          <a-select-option  style="width: 100%"  v-for="(item,index) in billEndGddayDataData" :key="index" :value="item">{{ item }}號</a-select-option>
                        </a-select>
                      </a-form-item>
                    </div>
                  </a-radio-group>
                </a-form-item>
           <a-divider type="vertical" class="divider-only"/>其他信息
               <a-form-item
                label="收租提醒"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                >
                    <a-radio-group placeholder="请选择收租提醒" button-style="solid" onch v-decorator="['rentReminderSwitch',{ initialValue: 1, rules: [{ required: true, message: '请选择收租提醒！' }]}]" @change="onRentReminderSwitchChange">
                      <a-radio-button  :key="0" :value="0">關閉</a-radio-button>
                      <a-radio-button  :key="1" :value="1">開啓</a-radio-button>
                    </a-radio-group>
              </a-form-item>
              <div v-show="onRentReminderSwitch">
                 <a-form-item
                  label="收租提醒提前 天"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                >
                  <a-select  style="width: 100%" v-decorator="['rentReminderDay', { initialValue: 1 , rules: [{ required: true, message: '请选择收租提醒提前 天！' }]}]">
                    <a-select-option  style="width: 100%"  v-for="(item,index) in 100" :key="index" :value="item">{{ item }}天</a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item
                  label="收租提醒提前 小時"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol">
                  <a-select  style="width: 100%" v-decorator="['rentReminderHour', { initialValue: 0 , rules: [{ required: true, message: '请选择收租提醒提前 小時！' }]}]">
                    <a-select-option  style="width: 100%"   :key="0" :value="0">0點</a-select-option>
                    <a-select-option  style="width: 100%"  v-for="(item,index) in 23" :key="index" :value="item">{{ item }}點</a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item
                  label="收租提醒提前 分鐘"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol">
                  <a-select  style="width: 100%" v-decorator="['rentReminderMinutes', { initialValue: 0 , rules: [{ required: true, message: '请选择收租提醒提前 分鐘！' }]}]">
                    <a-select-option  style="width: 100%"  :key="0" :value="0">0分</a-select-option>
                    <a-select-option  style="width: 100%"  v-for="(item,index) in 59" :key="index" :value="item">{{ item }}分</a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item
                  label="备注"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  has-feedback
                >
                  <a-textarea placeholder="请输入備注" v-decorator="['remark']" />
                </a-form-item>
              </div>
      </a-form>
    </a-spin>
    <add-live-form ref="addLiveForm" @ok="handleLiveOk"/>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { houseLeaseZkEdit, getTzrByLeaseId } from '@/api/modular/main/houselease/houseLeaseManage'
  import { sysFileInfoUpload, sysFileInfoPreview } from '@/api/modular/system/fileManage'
  import addLiveForm from './addLiveForm.vue'
  export default {
     components: {
      addLiveForm
    },
    data () {
      return {
           // 机构行样式
        labelCol_JG: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol_JG: {
          xs: { span: 24 },
          sm: { span: 20 }
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        rentStartDateDateString: '',
        rentEndDateDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        src1: '',
        src2: '',
        divLoading1: false,
        divLoading2: false,
        memberLoading: false,
        columns: [
          {
            title: '姓名',
            dataIndex: 'name',
            width: '15%',
            scopedSlots: { customRender: 'name' }
          },
          {
            title: '手機號',
            dataIndex: 'phone',
            width: '20%',
            scopedSlots: { customRender: 'phone' }
          },
          {
            title: '身份證號',
            dataIndex: 'idCard',
            width: '25%',
            scopedSlots: { customRender: 'idCard' }
          },
           {
            title: '照片正面',
            dataIndex: 'idCardImagesFront',
            width: '15%',
            scopedSlots: { customRender: 'idCardImagesFront' }
          },
          {
            title: '照片反面',
            dataIndex: 'idCardImagesBack',
            width: '15%',
            scopedSlots: { customRender: 'idCardImagesBack' }
          },
          {
            title: '操作',
            key: 'action',
            scopedSlots: { customRender: 'operation' }
          }
        ],
        // 同住人數據
        leaseLiveData: [],
         // 收租提醒开关
        onRentReminderSwitch: true,
        // 账单
        billEndGdmonthDataDataable: false,
        billEndTqDataable: false,
        radioStyle: {
          display: 'block',
          height: '30px',
          lineHeight: '30px'
        },
        billEndGdmonthDataData: [ { 'code': 12, 'name': '提前12月' }, { 'code': 11, 'name': '提前11月' }, { 'code': 10, 'name': '提前10月' }, { 'code': 9, 'name': '提前9月' }, { 'code': 8, 'name': '提前8月' }, { 'code': 7, 'name': '提前7月' }, { 'code': 6, 'name': '提前6月' }, { 'code': 5, 'name': '提前5月' }, { 'code': 4, 'name': '提前4月' }, { 'code': 3, 'name': '提前3月' }, { 'code': 2, 'name': '提前2月' }, { 'code': 1, 'name': '提前1月' }, { 'code': 0, 'name': '当月' }, { 'code': -1, 'name': '延後1月' }, { 'code': -2, 'name': '延後2月' }, { 'code': -3, 'name': '延後3月' }, { 'code': -4, 'name': '延後4月' }, { 'code': -5, 'name': '延後5月' }, { 'code': -6, 'name': '延後6月' }, { 'code': -7, 'name': '延後7月' }, { 'code': -8, 'name': '延後8月' }, { 'code': -9, 'name': '延後9月' }, { 'code': -10, 'name': '延後10月' }, { 'code': -11, 'name': '延後11月' }, { 'code': -12, 'name': '延後12月' } ],
        billEndGddayDataData: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 ]
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        this.getTzrListByLeaseId(record.id)
        this.onBillEndChange(record.billEndType)
        this.src1 = '/api/sysFileInfo/preview?id=' + record.idCardImagesFront
        this.src2 = '/api/sysFileInfo/preview?id=' + record.idCardImagesBack
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              roomId: record.roomId,
              name: record.name,
              phone: record.phone,
              idCard: record.idCard,
              receiptFu: record.receiptFu,
              receiptYj: record.receiptYj,
              billEndType: record.billEndType,
              billEndTqData: record.billEndTqData,
              billEndGdmonthData: record.billEndGdmonthData,
              billEndGddayData: record.billEndGddayData,
              rentReminderSwitch: record.rentReminderSwitch,
              rentReminderDay: record.rentReminderDay,
              rentReminderHour: record.rentReminderHour,
              rentReminderMinutes: record.rentReminderMinutes,
              remark: record.remark
            }
          )
        }, 100)
        if (record.rentReminderSwitch === 0) {
          this.onRentReminderSwitch = false
        }
      },
      getTzrListByLeaseId(id) {
        getTzrByLeaseId(id).then((res) => {
          if (res.code === 200) {
            this.leaseLiveData = res.data
          } else {
            this.$message.error('查詢失敗')//  + res.message
          }
        })
      },
      onRentReminderSwitchChange() {
        this.onRentReminderSwitch = !this.onRentReminderSwitch
      },
      beforeUpload(file) {
        const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
        if (!isJpgOrPng) {
          this.$message.error('只允許上傳圖片!')
        }
        const isLt2M = file.size / 1024 / 1024 < 6
        if (!isLt2M) {
          this.$message.error('圖片大小不允許超過6MB!')
        }
        return isJpgOrPng && isLt2M
      },
       /**
       * 上传文件
       */
      customRequest1 (data) {
        const formData = new FormData()
        formData.append('file', data.file)
        sysFileInfoUpload(formData).then((res) => {
          if (res.code === 200) {
            this.sysFileInfoPreview(res.data, '1')
            this.$nextTick(() => {
                this.form.setFieldsValue({
                  idCardImagesBack: res.data
                })
            })
          } else {
            this.$message.error('上传失败：' + res.msg)
          }
        })
      },
      customRequest2 (data) {
        const formData = new FormData()
        formData.append('file', data.file)
        sysFileInfoUpload(formData).then((res) => {
          if (res.code === 200) {
            this.sysFileInfoPreview(res.data, '2')
            this.$nextTick(() => {
                this.form.setFieldsValue({
                  idCardImagesFront: res.data
                })
            })
          } else {
            this.$message.error('上传失败：' + res.msg)
          }
        })
      },
      /**
       * 获取图片并转为链接
       */
      sysFileInfoPreview (id, type) {
        sysFileInfoPreview({ id: id }).then((res) => {
          this.downloadfile(res, type)
        }).catch((err) => {
          this.$message.error('预览错误：' + err.message)
        })
      },
      /**
       * 转图片类型
       */
      downloadfile (res, type) {
        const blob = new Blob([res])
        if (type === '1') {
          this.src1 = window.URL.createObjectURL(blob)
        } else {
          this.src2 = window.URL.createObjectURL(blob)
        }
      },
      // 添加同住人
      addYj() {
          this.$refs.addLiveForm.add(this.leaseLiveData.length)
      },
      /**
       * 删除
       */
      remove (key) {
        const newData = this.leaseLiveData.filter(item => item.key !== key)
        this.leaseLiveData = newData
      },
      handleLiveOk(values) {
        this.leaseLiveData.push(values)
      },
      onBillEndTypeChange (e) {
        if (e.target.value === 0) {
          this.billEndTqDataable = false
          this.billEndGdmonthDataDataable = true
        } else {
          this.billEndTqDataable = true
          this.billEndGdmonthDataDataable = false
        }
      },
      onBillEndChange (e) {
        if (e === 0) {
          this.billEndTqDataable = false
          this.billEndGdmonthDataDataable = true
        } else {
          this.billEndTqDataable = true
          this.billEndGdmonthDataDataable = false
        }
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.leaseLiveData = this.leaseLiveData
            houseLeaseZkEdit(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', '')
                this.handleCancel()
              } else {
                this.$message.error(res.msg)//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
        this.src1 = ''
        this.src2 = ''
        this.divLoading1 = false
        this.divLoading2 = false
        this.leaseLiveData = []
        this.onRentReminderSwitch = true
        this.billEndGdmonthDataDataable = false
        this.billEndTqDataable = false
      }
    }
  }
</script>
